package com.firefightingiot.firefightingiot.config.scheduling;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.support.utils.DayUtil;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceNormalLogMapper;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.UUID;

//@Component
public class TestScheduling {

    @Resource
    public MyLogger logger;

    @Resource
    DeviceNormalLogMapper deviceNormalLogMapper;
    @Resource
    DeviceBusinessMapper deviceBusinessMapper;

    @Scheduled(cron="0 */10 * * * ?")
    public void updateToOff() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("table", "device_normal_log_electric_fire_" + DayUtil.getToday());
        jsonObject.put("table", "device_normal_log_electric_fire_" + DayUtil.getToday());
        jsonObject.put("id", UUID.randomUUID().toString());
        jsonObject.put("device_id","04715ed7-394b-45f5-88c1-fdf458df5d03");
        jsonObject.put("number","863866047602917");

        String timeStr = DayUtil.getTodayAllTime();
        String times[] =  timeStr.split(" ");

        jsonObject.put("senddate", DateUtil.parseDate(times[0]));
        jsonObject.put("sendtime", times[1]);
        jsonObject.put("business_id","40be6647-87e9-4449-bdc2-7d0ff2e741a1");
        jsonObject.put("business_name","测试单位");
        jsonObject.put("a_voltage_value",getStrForV(220.0));
        jsonObject.put("b_voltage_value",getStrForV(220.0));
        jsonObject.put("c_voltage_value",getStrForV(220.0));

        jsonObject.put("a_electric_value",getStrForL(1.5));
        jsonObject.put("b_electric_value",getStrForL(1.5));
        jsonObject.put("c_electric_value",getStrForL(1.5));

        jsonObject.put("dual_electric_value",getStrForL(60.0));

        jsonObject.put("a_temp_value",getStrForL(13.2));
        jsonObject.put("b_temp_value",getStrForL(12.3));
        jsonObject.put("c_temp_value",getStrForL(15.6));
        jsonObject.put("n_temp_value",getStrForL(13.6));

        jsonObject.put("a_active_power",getStrForV(0.3));
        jsonObject.put("b_active_power",getStrForL(0.0));
        jsonObject.put("c_active_power",getStrForL(0.3));

        jsonObject.put("a_reactive_power",getStrForL(0.0));
        jsonObject.put("b_reactive_power",getStrForL(0.0));
        jsonObject.put("c_reactive_power",getStrForV(0.0));

        jsonObject.put("a_apparent_power",getStrForL(0.0));
        jsonObject.put("b_apparent_power",getStrForL(0.0));
        jsonObject.put("c_apparent_power",getStrForL(0.0));

        jsonObject.put("a_power_factor",getStrForL(0.0));
        jsonObject.put("b_power_factor",getStrForL(0.0));
        jsonObject.put("c_power_factor",getStrForL(0.0));

        jsonObject.put("all_active_power",getStrForL(2.0));
        jsonObject.put("all_reactive_power",getStrForL(1.5));
        jsonObject.put("all_apparent_power",getStrForL(2.3));
        jsonObject.put("all_power_factor",getStrForL(1.8));

        jsonObject.put("a_voltage_harmonic",getStrForL(0.0));
        jsonObject.put("b_voltage_harmonic",getStrForL(0.0));
        jsonObject.put("c_voltage_harmonic",getStrForL(0.0));

        jsonObject.put("a_electric_harmonic",getStrForL(0.0));
        jsonObject.put("b_electric_harmonic",getStrForL(0.0));
        jsonObject.put("c_electric_harmonic",getStrForL(0.0));

        deviceNormalLogMapper.inserDeviceNormalLogElectricFire(jsonObject);

        DeviceBusiness DeviceBusiness = new DeviceBusiness();
        DeviceBusiness.setNormalUpdateTime(DateUtil.now());
        DeviceBusiness.setCurrentstate("1");
        DeviceBusiness.setId("04715ed7-394b-45f5-88c1-fdf458df5d03");
        deviceBusinessMapper.updateById(DeviceBusiness);
    }

    //生成随机小数（5以内）电压
    public String getStrForV(Double b){
        double a= Math.random()*5;
        DecimalFormat df = new DecimalFormat( "0.00" );
        String str=df.format( a+b );
        return str;
    }
    //生成随机小数（1以内）电流
    public String getStrForL(Double b){
        double a= Math.random()/10;
        DecimalFormat df = new DecimalFormat( "0.00" );
        String str=df.format( a+b );
        return str;
    }

    public static void main(String[] args) {
        String timeStr = DayUtil.getTodayAllTime();
        String times[] =  timeStr.split(" ");
        System.out.println(times[0]+"--------"+times[1]);
    }
}
